<?php

namespace frontend\modules\general\controllers;

use frontend\modules\common\controllers\BaseController;
use frontend\modules\common\models\BaseHelper;
use frontend\modules\resource\logic\SpecialLogic;
use Yii;
use common\models\HlSpecial;
use frontend\modules\resource\models\SpecialSearch;
use yii\web\NotFoundHttpException;

/**
 * SpecialController implements the CRUD actions for HlSpecial model.
 */
class SpecialController extends BaseController
{

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        $this->branchAdmin();
    }

    public function actions()
    {
        return [
            'upload' => [
                'class' => 'kucha\ueditor\UEditorAction',
            ]
        ];
    }


    /**
     * Lists all HlSpecial models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new SpecialSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams, $this->branchId);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }


    /**
     * @return string|\yii\web\Response
     * @throws \yii\base\ExitException
     */
    public function actionCreate()
    {
        $model = new HlSpecial();

        if (Yii::$app->request->post()) {
            $res = (new SpecialLogic())->saveSpecialInfo($model, $this->branchId, $this->branchId);
            if ($res) {
                return $this->redirect(['finish']);
            }
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * @param $id
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException
     * @throws \yii\base\ExitException
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        (new SpecialLogic())->checkSId($id, $this->branchId);
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['finish']);
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * @param $id
     * @return string|\yii\web\Response
     * @throws NotFoundHttpException
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function actionDelete()
    {
        $id = Yii::$app->request->post('id');

        //不是数字 返回错误
        if (!is_numeric($id)) return $this->err();
        HlSpecial::updateAll(['status' => 0], ['id' => $id]);

        //如果是ajax请求
        if (Yii::$app->request->isAjax) return $this->suc();
        return $this->redirect(['index']);
    }

    /**
     * Finds the HlSpecial model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return HlSpecial the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = HlSpecial::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    /**
     * 专题推荐
     * @return string
     * @throws \yii\base\ExitException
     */
    public function actionSpecialSupport()
    {
        $id = Yii::$app->request->post('id');
        (new SpecialLogic())->checkSId($id, $this->branchId);
        $res = (new SpecialLogic())->UpdateSupport($id, $this->branchId);
        if (!$res) {
            BaseHelper::Error('修改失败');
        }
        return $this->suc(['msg' => '修改成功']);

    }
}
